package cn.edu.zjgsu.ito.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;






import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import cn.edu.zjgsu.ito.model.AdminInfomation;
import cn.edu.zjgsu.ito.model.AdminLogin;
import cn.edu.zjgsu.ito.model.AdminVo;
import cn.edu.zjgsu.ito.model.CourierVo;
import cn.edu.zjgsu.ito.model.OrderDetailInfomationView;
import cn.edu.zjgsu.ito.model.UserInfomation;
import cn.edu.zjgsu.ito.model.UserVo;
import cn.edu.zjgsu.ito.service.IAdminService;
import cn.edu.zjgsu.ito.util.Constant;
import cn.edu.zjgsu.ito.util.Md5Util;
import cn.edu.zjgsu.ito.vo.OrderDetailInfomationVo;
import cn.edu.zjgsu.ito.vo.OrderStatisticalVo;
import cn.edu.zjgsu.ito.vo.Page;

@RestController
@RequestMapping(value = "/AdminController")
@Transactional
public class AdminController
{
	@Autowired
	private IAdminService adminService;

	/**
	 * 管理员登录,成功返回code"200"以及role权限,用户名或密码错误返回code"500"
	 * 
	 * @param adminName
	 * @param adminPwd
	 * @return
	 */
	@RequestMapping(value = "/admin/login", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> login(@RequestBody AdminLogin adminLogin,HttpServletRequest request)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		// System.out.println("加密后的密码为: "+Md5Util.getMD5(adminLogin.getAdminPwd()));
		Map<String,Object> selectResult = adminService.login(adminLogin.getAdminName(),
				adminLogin.getAdminPwd());
		switch ((Integer) selectResult.get("result"))
		{
		case -1:
			result.put("code", Constant.FAIL);
			break;
		case 1:
			HttpSession session = request.getSession();
			session.setAttribute("adminName", ((AdminInfomation)selectResult.get("adminInfomation")).getAdminName());
			result.put("code", Constant.OK);
			result.put("admin",
					JSON.toJSON(selectResult.get("adminInfomation")));
			break;
		default:
			break;
		}
		return result;
	}

	/**
	 * 管理员登出
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/admin/logout", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> login(HttpServletRequest request)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		HttpSession session = request.getSession();
		Enumeration<String> attributes = session.getAttributeNames();
		while (attributes.hasMoreElements())
		{
			session.removeAttribute(attributes.nextElement());
		}
		result.put("code", Constant.OK);
		return result;
	}
	
	
	
	/**
	 * 资金进入量
	 * @return
	 */
	@RequestMapping(value = "/inflowMoney", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> inflowMoney()
	{
		Map<String, Object> result = new HashMap<String, Object>();
		// 订单状态 0-等待代购人确定金额 1-确认，未支付 2-已支付 3-找快递员 4-待取件 5-派送中 6-完成未评价 7-关闭 8-退款中  9-结束
				List<Integer> states = new ArrayList<Integer>()
				{
					{
						add(2);
						add(3);
						add(4);
						add(5);
						add(6);
						add(8);
						add(9);
					}
				};
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				java.util.Date beginTime = calendar.getTime();
				calendar.add(Calendar.DATE, 1);
				java.util.Date endTime = calendar.getTime();
		result.put("money", adminService.selectMoneyIn(beginTime,endTime,states));
		result.put("code", Constant.OK);
		return result;
	}
	
	/**
	 * 资金流出量
	 * @return
	 */
	@RequestMapping(value = "/outflowMoney", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> outflowMoney()
	{
		Map<String, Object> result = new HashMap<String, Object>();
		// 订单状态 0-等待代购人确定金额 1-确认，未支付 2-已支付 3-找快递员 4-待取件 5-派送中 6-完成未评价 7-关闭 8-退款中  9-结束
				List<Integer> states = new ArrayList<Integer>()
				{
					{
						add(6);
						add(9);
					}
				};
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				java.util.Date beginTime = calendar.getTime();
				calendar.add(Calendar.DATE, 1);
				java.util.Date endTime = calendar.getTime();
		result.put("money", adminService.selectMoneyOut(beginTime,endTime,states));
		result.put("code", Constant.OK);
		return result;
	}
	
	
	
	
	
	
	/**
	 * 查询订单top用户
	 * @param number
	 * @return
	 */
	@RequestMapping(value = "/topUser/{number}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTopUser(@PathVariable int number)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		List<UserInfomation> UserInfomations = adminService.selectTopUser(number);
		result.put("users", UserInfomations);
		result.put("code", Constant.OK);
		return result;
	}
	
	
	
	
	/**
	 * 查询订单top快递员
	 * @param number
	 * @return
	 */
	@RequestMapping(value = "/topCourier/{number}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTopCourier(@PathVariable int number)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		List<CourierVo> CourierVos = adminService.selectTopCourier(number);
		result.put("couriers", CourierVos);
		result.put("code", Constant.OK);
		return result;
	}
	
	
	
	
	
	/**
	 * 查询各种状态的订单数量
	 * @return
	 */
	@RequestMapping(value = "/differentStateOrderNumber", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getDifferentStateOrderNumber()
	{
		Map<String, Object> result = new HashMap<String, Object>();
		/*订单状态 -1全部  0-等待代购人确定金额  1-确认，未支付 2-已支付  3-找快递员 4-待取件 5-派送中  6-完成未评价  7-关闭  8-退款中 9-结束*/
		int[] states = new int[]{0,1,2,3,4,5,6,7,8,9};
		List<Integer> counts = new ArrayList<Integer>();
		for(int state:states)
		{
			counts.add(adminService.selectOrderCountByState(state));
		}
		result.put("counts", counts);
		result.put("message", "订单状态   0-等待代购人确定金额  1-确认，未支付 2-已支付  3-找快递员 4-待取件 5-派送中  6-完成未评价  7-关闭  8-退款中 9-结束");
		result.put("code", Constant.OK);
		return result;
	}
	
	
	
	
	
	
	
	/**
	 * 得到今日订单数
	 * 
	 * @param requestJson
	 * @return
	 */
	@RequestMapping(value = "/todayOrderCount", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTodayOrderCount()
	{
		Map<String, Object> result = new HashMap<String, Object>();
		int resultCount = adminService.getTodayOrderCount();
		result.put("code", Constant.OK);
		result.put("count", resultCount);
		return result;
	}

	/**
	 * 得到订单数量,即历史订单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/totalOrder", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTotalOrder()
	{
		Map<String, Object> result = new HashMap<String, Object>();
		int totalOrderCount = adminService.getTotalOrderCount();
		result.put("code", Constant.OK);
		result.put("count", totalOrderCount);
		return result;
	}

	/**
	 * 得到用户总数
	 * 
	 * @return
	 */
	@RequestMapping(value = "/totalUser", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTotalUser()
	{
		Map<String, Object> result = new HashMap<String, Object>();
		int totalUserCount = adminService.getTotalUserCount();
		result.put("code", Constant.OK);
		result.put("count", totalUserCount);
		return result;
	}

	/**
	 * 得到快递员总数
	 * 
	 * @return
	 */
	@RequestMapping(value = "/totalCourier", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTotalCourier()
	{
		Map<String, Object> result = new HashMap<String, Object>();
		int totalCourierCount = adminService.getTotalCourierCount();
		result.put("code", Constant.OK);
		result.put("count", totalCourierCount);
		return result;
	}

	/**
	 * 由当前日期开始,取得之前若干天的活跃用户数量
	 * 
	 * @param day
	 * @return
	 */
	@RequestMapping(value = "/usersNumberByDay/{day}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUsersNumberByDay(@PathVariable int day)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		List<Integer> resultList = adminService.getUsersNumberByDay(day);
		result.put("code", Constant.OK);
		result.put("numberList", resultList);
		return result;
	}

	/**
	 * 管理员相关 分页根据关键字查询管理员并排序(暂无排序)
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param sequence
	 *            (1全部,2超管,3普管)
	 * @return
	 *//*
	@RequestMapping(value = "/admins/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAdmins(@PathVariable int pageCount,
			@PathVariable int pageSize,
			@RequestParam(name = "sequence") int sequence,
			@RequestParam(name = "key", required = false) String key)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		Page<AdminVo> admins = adminService.getAdminsWithPaging(pageCount,
				pageSize, sequence, key == null ? "" : key);
		result.put("admins", admins.getData());
		result.put("count", admins.getTotalCount());
		return success(result);
	}*/
	
	/**
	 * 管理员相关 分页根据关键字查询管理员并排序(暂无排序)
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param sequence
	 *            (1全部,2超管,3普管)
	 * @return
	 */
	@RequestMapping(value = "/admins/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAdmins(
			@RequestParam(name = "sequence") int sequence)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		List<AdminVo> admins = adminService.getAdmins(sequence);
		result.put("admins", admins);
		return success(result);
	}
	
	
	

	/**
	 * 用户相关 分页根据关键字查询用户并排序(暂无排序)
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param sequence
	 * @return
	 */
	/*
	 * @RequestMapping(value =
	 * "/users/pageCount/{pageCount}/pageSize/{pageSize}", method =
	 * RequestMethod.GET)
	 * 
	 * @ResponseBody public Map<String, Object> getUsers(@PathVariable int
	 * pageCount,
	 * 
	 * @PathVariable int pageSize,
	 * 
	 * @RequestParam(name = "sequence") int sequence,
	 * 
	 * @RequestParam(name = "key", required = false) String key) { Map<String,
	 * Object> result = new HashMap<String, Object>(); List<UserVo> userVos =
	 * adminService.getUsersWithPaging(pageCount, pageSize, sequence, key ==
	 * null ? "" : key); result.put("code", Constant.OK); result.put("userVo",
	 * userVos); result.put("count", adminService.selectCount(6, 0, key,
	 * sequence)); return result; }
	 */

	/**
	 * 用户相关 分页根据关键字查询用户并排序(暂无排序)
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param sequence
	 * @return
	 *//*
	@RequestMapping(value = "/users/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUsers(@PathVariable int pageCount,
			@PathVariable int pageSize,
			@RequestParam(name = "sequence") int sequence,
			@RequestParam(name = "key", required = false) String key)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		Page<UserVo> userVos = adminService.getUsersWithPaging(pageCount,
				pageSize, sequence, key == null ? "" : key);
		result.put("userVo", userVos.getData());
		result.put("count", userVos.getTotalCount());
		return success(result);
	}*/
	
	
	
	/**
	 * 用户相关 分页根据关键字查询用户并排序(暂无排序)
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param sequence
	 * @return
	 */
	@RequestMapping(value = "/users/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUsers(@RequestParam(name = "sequence") int sequence)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		/*Page<UserVo> userVos = adminService.getUsersWithPaging(pageCount,
				pageSize, sequence, key == null ? "" : key);*/
		List<UserVo> userList = adminService.getUsers(sequence);
		result.put("userVo", userList);
		//result.put("count", userVos.getTotalCount());
		return success(result);
	}
	
	
	
	
	
	
	

	/**
	 * 根据userId查看用户的详细信息
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserInfoByUserId(
			@PathVariable(name = "userId") int userId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", Constant.OK);
		result.put("user", adminService.getUserByUserId(userId));
		return result;
	}

	/**
	 * 快递员相关 根据条件分页查找快递员
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param sequence
	 *            (1接单数从小到大,2接单数从大到小,3总评分由低到高,4总评分由高到低,5创建时间最近的排最前,6.创建时间最早的排最前)
	 * @param key
	 * @return
	 */
	/*
	 * @RequestMapping(value =
	 * "/couriers/pageCount/{pageCount}/pageSize/{pageSize}", method =
	 * RequestMethod.GET)
	 * 
	 * @ResponseBody public Map<String, Object> getCouriers(@PathVariable int
	 * pageCount,
	 * 
	 * @PathVariable int pageSize,
	 * 
	 * @RequestParam(name = "sequence") int sequence,
	 * 
	 * @RequestParam(name = "key", required = false) String key) { Map<String,
	 * Object> result = new HashMap<String, Object>(); List<CourierVo>
	 * courierVos = adminService.getCouriersWithPaging( pageCount, pageSize,
	 * sequence, key == null ? "" : key); result.put("code", Constant.OK);
	 * result.put("courierVos", courierVos); return result; }
	 */

	/**
	 * 快递员相关 根据条件分页查找快递员
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param sequence
	 *            (1接单数从小到大,2接单数从大到小,3总评分由低到高,4总评分由高到低,5创建时间最近的排最前,6.创建时间最早的排最前)
	 * @param key
	 * @return
	 *//*
	@RequestMapping(value = "/couriers/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getCouriers(@PathVariable int pageCount,
			@PathVariable int pageSize,
			@RequestParam(name = "sequence") int sequence,
			@RequestParam(name = "key", required = false) String key)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		Page<CourierVo> page = adminService.getCouriersWithPaging(pageCount,
				pageSize, sequence, key == null ? "" : key);
		result.put("courierVos", page.getData());
		result.put("count", page.getTotalCount());
		return success(result);
	}
	*/
	
	
	/**
	 * 快递员相关 根据条件分页查找快递员
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param sequence
	 *            (1接单数从小到大,2接单数从大到小,3总评分由低到高,4总评分由高到低,5创建时间最近的排最前,6.创建时间最早的排最前)
	 * @param key
	 * @return
	 */
	@RequestMapping(value = "/couriers/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getCouriers(@RequestParam(name = "sequence") int sequence)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		List<CourierVo> couriers = adminService.getCouriers(sequence);
		result.put("courierVos", couriers);
		return success(result);
	}
	
	
	
	

	/**
	 * 根据快递员courierId分页查询订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param courier
	 * @param sequence
	 * @param key
	 * @return
	 */
	/*
	 * @RequestMapping(value =
	 * "/courierOrder/pageCount/{pageCount}/pageSize/{pageSize}", method =
	 * RequestMethod.GET)
	 * 
	 * @ResponseBody public Map<String, Object>
	 * getOrdersByCourierId(@PathVariable int pageCount,
	 * 
	 * @PathVariable int pageSize,
	 * 
	 * @RequestParam(name="courierId") int courierId,
	 * 
	 * @RequestParam(name = "sequence") int sequence,
	 * 
	 * @RequestParam(name = "key", required = false) String key) { Map<String,
	 * Object> result = new HashMap<String, Object>(); List<OrderStatisticalVo>
	 * OrderStatisticalVos = adminService.getOrderByCourierId(courierId,
	 * sequence, key, pageCount, pageSize); result.put("code", Constant.OK);
	 * result.put("OrderStatisticalVos", OrderStatisticalVos);
	 * result.put("count", adminService.selectCount(key, sequence, courierId));
	 * return result; }
	 */

	/**
	 * 根据快递员courierId分页查询订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param courier
	 * @param sequence
	 * @param key
	 * @return
	 *//*
	@RequestMapping(value = "/courierOrder/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getOrdersByCourierId(
			@PathVariable int pageCount, @PathVariable int pageSize,
			@RequestParam(name = "courierId") int courierId,
			@RequestParam(name = "sequence") int sequence,
			@RequestParam(name = "key", required = false) String key)
	{
		Map<String, Object> result = new HashMap<String, Object>();

		
		 * Page<OrderStatisticalVo> OrderStatisticalVos = adminService
		 * .getOrderByCourierId(courierId, sequence, key, pageCount, pageSize);
		 

		Page<OrderDetailInfomationView> page = adminService
				.getOrderByCourierId(courierId, sequence, key, pageCount,
						pageSize);

		result.put("OrderStatisticalVos", page.getData());
		result.put("count", page.getTotalCount());
		return success(result);
	}*/
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	/**
	 * 根据快递员courierId得到快递员个人信息
	 * 
	 * @param courierId
	 * @return
	 */
	@RequestMapping(value = "/courier/{courierId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getCourierById(
			@PathVariable(name = "courierId") int courierId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		CourierVo courierVo = adminService.getCourierByCourierId(courierId);
		result.put("code", Constant.OK);
		result.put("courier", courierVo);
		return result;
	}

	/**
	 * 批量关闭管理员账户
	 * 
	 * @param adminId
	 * @return
	 */
	@RequestMapping(value = "/closeAdmins", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> closeAccouts(@RequestBody Map requestJson)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		ArrayList<Integer> adminIds = (ArrayList<Integer>) requestJson
				.get("adminIds");
		// 0为关闭
		int adminState = 0;
		int resultCount;
		for (int adminId : adminIds)
		{
			resultCount = adminService.closeAdmin(adminId, adminState);
			if (resultCount != 1)
			{
				throw new RuntimeException();
			}
		}
		result.put("code", Constant.OK);
		return result;
	}

	/**
	 * 关闭单个管理员账户
	 * 
	 * @param adminId
	 * @return
	 */
	@RequestMapping(value = "/closeAdmin/{adminId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> closeAccout(@PathVariable int adminId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		// 0为关闭
		int adminState = 0;
		int resultCount;
		resultCount = adminService.closeAdmin(adminId, adminState);
		if (resultCount != 1)
		{
			throw new RuntimeException();
		}
		result.put("code", Constant.OK);
		return result;
	}

	/**
	 * 打开单个管理员账户
	 * 
	 * @param adminId
	 * @return
	 */
	@RequestMapping(value = "/openAdmin/{adminId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> openAccout(@PathVariable int adminId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		// 1为打开
		int adminState = 1;
		int resultCount;
		resultCount = adminService.closeAdmin(adminId, adminState);
		if (resultCount != 1)
		{
			throw new RuntimeException();
		}
		result.put("code", Constant.OK);
		return result;
	}

	/**
	 * 增加管理员
	 * 
	 * @param adminVo
	 * @return
	 */
	@RequestMapping(value = "/Admin", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addAdmin(@RequestBody AdminVo adminVo)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		if (adminService.checkAdminName(adminVo.getAdminName()) == 1)
		{
			result.put("code", Constant.FAIL);
			result.put("result", "用户名已存在");
			return result;
		}
		int affectedCount = adminService.addAdmin(adminVo);
		if (affectedCount == 1)
		{
			result.put("code", Constant.OK);
		} else
		{
			result.put("code", Constant.FAIL);
		}
		return result;
	}
	
	
	

	/**
	 * 删除管理员
	 * 
	 * @param requestJson
	 * @return
	 */
	@RequestMapping(value = "/Admin/{adminId}", method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String, Object> deleteAdmin(
			@PathVariable(name = "adminId") int adminId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		int resultCount = adminService.delAdmin(adminId);
		switch (resultCount)
		{
		case 0:
			result.put("code", Constant.FAIL);
			break;
		case 1:
			result.put("code", Constant.OK);
		default:
			break;
		}
		return result;
	}

	/**
	 * 根据adminId查询管理员信息
	 * 
	 * @param adminId
	 * @return
	 */
	@RequestMapping(value = "/Admin/{adminId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAdminById(
			@PathVariable(name = "adminId") int adminId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		AdminInfomation adminInfomation = adminService.getAdminById(adminId);
		if (adminInfomation != null)
		{
			result.put("code", Constant.OK);
			result.put("admin", adminInfomation);
		} else
		{
			result.put("code", Constant.FAIL);
		}
		return result;
	}

	/**
	 * 修改管理员(不包含密码)
	 * 
	 * @param adminInfomation
	 * @return
	 */
	@RequestMapping(value = "/Admin", method = RequestMethod.PUT)
	@ResponseBody
	public Map<String, Object> updateAdminWhithoutPassword(
			@RequestBody AdminInfomation adminInfomation)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		if (adminService.getAdminById(adminInfomation.getAdminId())
				.getAdminName() != adminInfomation.getAdminName()
				&& adminService.checkAdminName(adminInfomation.getAdminName()) == 1)
		{
			result.put("code", Constant.FAIL);
			result.put("result", "用户名已存在");
			return result;
		}
		int resultCount = adminService
				.updateAdminWithoutPassword(adminInfomation);
		if (resultCount == 1)
		{
			result.put("code", Constant.OK);
		} else
		{
			result.put("code", Constant.FAIL);
		}
		return result;
	}

	/**
	 * 修改自己信息(带密码)
	 * 
	 * @param amdinVo
	 * @return
	 */
	@RequestMapping(value = "/updateAdminItself", method = RequestMethod.PUT)
	@ResponseBody
	public Map<String, Object> updateAdminItself(@RequestBody AdminVo amdinVo)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		if (adminService.getAdminById(amdinVo.getAdminId()).getAdminName() != amdinVo
				.getAdminName()
				&& adminService.checkAdminName(amdinVo.getAdminName()) == 1)
		{
			result.put("code", Constant.FAIL);
			result.put("result", "用户名已存在");
		}
		int resultCount = adminService.updateAdminItself(amdinVo);
		if (resultCount == 1)
		{
			result.put("code", Constant.OK);
		} else
		{
			result.put("code", Constant.FAIL);
		}
		return result;
	}

	/**
	 * 重置管理密码
	 * 
	 * @param adminId
	 * @return
	 */
	@RequestMapping(value = "/resetPassword/{adminId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> resetPassword(
			@PathVariable(name = "adminId") int adminId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		int resultCount = adminService.resetPassowrd(adminId, "123456");
		if (resultCount == 1)
			result.put("code", Constant.OK);
		else
			result.put("code", Constant.FAIL);
		return result;
	}

	/**
	 * 冻结用户
	 * 
	 * @return
	 */
	@RequestMapping(value = "/closeAccount/{userId}", method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String, Object> closeUserAccount(@PathVariable int userId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		adminService.changeAccountState(userId,0);
		return success(result);
	}

	
	
	/**
	 * 解冻用户
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/openAccount/{userId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> openUserAccount(@PathVariable int userId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		adminService.changeAccountState(userId, 1);
		return success(result);

	}
	
	/**
	 * 冻结快递员
	 * @param courierId
	 * @return
	 */
	@RequestMapping(value = "/closeCourier/{courierId}", method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String, Object> closeCourier(@PathVariable int courierId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		adminService.changeCourierState(courierId,0);
		return success(result);
	}
	
	/**
	 * 解冻快递员
	 * @param courierId
	 * @return
	 */
	@RequestMapping(value = "/openCourier/{courierId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> openCourier(@PathVariable int courierId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		adminService.changeCourierState(courierId,1);
		return success(result);
	}
	
	

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> test()
	{
		Map<String, Object> result = new HashMap<String, Object>();
		int money = adminService.test();
		result.put("money", money);
		return result;
	}

	private Map<String, Object> error(Map<String, Object> result)
	{
		result.put("code", Constant.FAIL);
		return result;
	}

	private Map<String, Object> success(Map<String, Object> result)
	{
		result.put("code", Constant.OK);
		return result;
	}

}
